var util = require('../utils/util.js');

(function() {
   function getStoreSync(key) {
       try {
            return wx.getStorageSync(key);
        }
        catch(e){
            return null;
        }
   }

   function setStoreSync(object) {
       try {
            wx.setStorgaeSync(object);
            return true;
        }
        catch(e){
            return false;
        }
   }

   var store = {
       data: {
           itemList:{},
           collectList:[],
       },
       defaultListText: ['向右滑动标记事项完成', '向左滑动删除事项', '下拉创建事项', '上拉清理已完成事项', '试一试长按', '下拉更多切换到集合列表' ],
       init: function(ctx, onGetData, onSetData) {
           this.ctx = ctx;
           this.onGetData = onGetData;
           this.onSetData = onSetData;
       }, 

       getSetup: function() {
          return getStoreSync('wx-clear-setup');
       },

       saveSetup: function(data) {
          var s = this;
          var object = {
              key: 'wx-clear-setup',
              data: data,
              success: function() {
                  s.onSetData.call(s.ctx, true, 'wx-clear-setup', data);
              }, 
              fail: function() {
                  s.onSetData.call(s.ctx, false,'wx-clear-setup');
              }
           }
           wx.setStorage(object);
       },
       
       setSetup: function(data) {
          return setStoreSync('setup', data);
       },

       getItemList: function(id) {
           return this.data.itemList[id];
       },

       getItemListFromStore: function(key) {
           var object = {
               key: key,
               success: function(res) {
                   this.data.itemList[key] = res.data || [];
                   this.onGetData.call(this.ctx, true, key, data);
               },
               fail: function() {
                   this.onGetData.call(this.ctx, false, key);
               }
           }
           wx.getStorgae(object);
       },

       getItemListFromStoreSync: function(key) {
           return getStoreSync(key);
       },

       saveItemList: function(key, data) {
           if (!key || !data) {
               return;
           }
           var s = this;
           var object = {
               key: key,
               data: data,
               success: function() {
                   s.data.itemList[key] = data;
                   s.onSetData.call(s.ctx, true, key, data);
               },
               fail: function() {
                   s.onSetData.call(s.ctx, false, key);
               }
           }
           wx.setStorage(object);
       },

       getCollectList: function() {
           return this.data.collectList;
       },

       getCollectFromStore: function() {
           var s = this;
           var object = {
               key: 'collect-list',
               success: function(res) {
                   s.data.collectList = res.data || [];
                   s.onGetData.call(s.ctx, true, 'collect-list', data);
               }, 
               fail: function() {
                   s.onGetData.call(s.ctx, false, 'collect-list');
               }
           }
           wx.getStorgae(object);
       },

       getCollectFromStoreSync: function() {
           return getStoreSync('collect-list');
       },

       saveCollectList: function (collectList) {
           var s = this;
           var data = collectList;
           var object = {
               key: 'collect-list',
               data: data,
               success: function() {
                   s.onSetData.call(s.ctx, true, 'collect-list', data);
               }, 
               fail: function() {
                   s.onSetData.call(s.ctx, false, 'collect-list');
               }
           }
           wx.setStorage(object);
       },

       buildDefaultData: function() {
           var collectList = [];
           var now = Date.now();
           var time = 'p' + now;
           var personalList = {
               id:util.generateCollectId(time),
               type:util.TYPE_COLLECT,
               order:0,
               lineCount:1,
               count:6,
               doneCount:0,
               text:'欢迎使用',
               createTime:now,
               lastPos:0
           };
           collectList.push(personalList);

           var itemList = [];
           now = Date.now();
           for (var i = 0; i < 6; i++) {
               var item = {};
               item.id = util.generateItemId(now);
               item.order = i;
               item.lineCount = 1;
               item.createTime = now;
               item.type = util.TYPE_ITEM;
               item.state = util.STATE_NORMAL;
               item.text = this.defaultListText[i];
               item.remindTime = 0;
               itemList.push(item);
               now++;
           }
           var data = {};
           data.collectList = collectList;
           data.itemList = itemList;
           data.collectId = personalList.id;
           return data;
       },

       createNewItem: function(text, order, lineCount, remindTime) {
           var now = Date.now();
           var item = {};
           item.id = util.generateItemId(now);
           item.order = order;
           item.lineCount = lineCount;
           item.createTime = now;
           item.type = util.TYPE_ITEM;
           item.state = util.STATE_NORMAL;
           item.text = text;
           item.remindTime = 0;

           return item;
       },

       createNewCollect: function(text, order, lineCount, count) {
           var now = Date.now();
           var item = {};
           item.id = util.generateCollectId(now),
           item.type = util.TYPE_COLLECT;
           item.order = order;
           item.lineCount = lineCount;
           item.count = count;
           item.doneCount = 0;
           item.text = text;
           item.createTime = now;
           item.lastPos = 0;

           return item;
       },

   };
   module.exports = store;
})();